﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Domain.Common;
using Domain.Map;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MapComponentSprite
{
    /// <summary>
    /// 旗子
    /// </summary>
    public class FlagSprite : AutomatedSprite
    {
        /// <summary>
        /// 只有一个旗子图片，一个实例就可以了
        /// </summary>
        public static Texture2D FlagImage { get; set; }
        /// <summary>
        /// 旗子上面的字
        /// </summary>
        public string Text { get; set; }
        public FlagTextSprite FlagTextSprite;
        public FlagSprite(string text)
        {
            this.FrameSize = new Point(100, 100);
            this.SheeftSize = new Point(6, 1);
            this.CurrentFrame = new Point(0, 0);
            LoadPic();
            this.Image = FlagImage;
            this.Text = text;
            if (!string.IsNullOrWhiteSpace(text))
                FlagTextSprite = new FlagTextSprite(text);
        }
        public void Draw(GameTime gameTime,Color color)
        {
            SpriteBatch.Draw(Image, DrawPosition,
                                       new Rectangle(CurrentFrame.X * FrameSize.X,
                                         CurrentFrame.Y * FrameSize.Y,
                                         FrameSize.X,
                                         FrameSize.Y),
                                   color, 0, Vector2.Zero, SpriteEffects.None, 0.79998f);
            if (FlagTextSprite != null)
                FlagTextSprite.Draw(gameTime);
        }

        public void Update(GameTime gameTime, Point position)
        {
            this.DrawPosition = new Rectangle(position.X, position.Y, MapUnit.Width, MapUnit.Height);
            Rectangle rectangle = new Rectangle();
            rectangle.X = (position.X) * MapUnit.Width + GameScreen.LeftEdge;
            rectangle.Y = (position.Y - 1) * MapUnit.Height + GameScreen.TopEdge;
            rectangle.Width = MapUnit.Width;
            rectangle.Height = MapUnit.Height;
            this.DrawPosition = rectangle;
            //更新位图
            Point newp = CurrentFrame;
            ++newp.X;
            if (newp.X >= SheeftSize.X)
            {
                newp.X = 0;
                ++newp.Y;
                if (newp.Y >= SheeftSize.Y)
                    newp.Y = 0;
            }
            CurrentFrame = newp;
            if (FlagTextSprite != null)
                FlagTextSprite.Update(gameTime, DrawPosition);
        }

        public void LoadPic()
        {
            if (FlagImage == null)
                FlagImage = Game.Content.Load<Texture2D>(@"Images\buildings\type\qizi");
        }
    }
}
